package com.cykj.pos.service.impl;

import com.cykj.pos.domain.BizDayStatistics;
import com.cykj.pos.domain.BizMerchant;
import com.cykj.pos.domain.BizWallet;
import com.cykj.pos.mapper.BizMerchantInfoMapper;
import com.cykj.pos.service.IBizDayStatisticsService;
import com.cykj.pos.service.IBizMicroInfoService;
import com.cykj.pos.service.IBizWithdrawalRecordsSpService;
import com.cykj.pos.util.DESHelperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 孙总家代付记录Service业务层处理
 *
 * @author xxl
 * @date 2024-04-12
 */
@Service
public class BizWithdrawalRecordsSpServiceImpl  implements IBizWithdrawalRecordsSpService {

    @Autowired
    private com.cykj.pos.domain.PosPCommonParamConfig PosPCommonParamConfig;



    @Autowired
    private BizMerchantInfoMapper  bizMerchantInfoMapper;

    @Autowired
    private BizMerchantServiceImpl bizMerchantServiceImpl;

    @Autowired
    private IBizDayStatisticsService iBizDayStatisticsService;
    @Autowired
    private IBizMicroInfoService bizMicroInfoService;




    @Override
    public void statisticsEveryDay() {
        List<BizWallet> walletList = bizMerchantInfoMapper.getWalletList();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //获取昨天的日期
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(new Date());
        rightNow.add(Calendar.DAY_OF_MONTH, -1);
        Date dt1 = rightNow.getTime();
        String yesterday = sdf.format(dt1);
        //获取当天的日期
        Calendar rightNowToday = Calendar.getInstance();
        rightNowToday.setTime(new Date());
        rightNowToday.add(Calendar.DAY_OF_MONTH, -0);
        Date dt2 = rightNowToday.getTime();
        String today = sdf.format(dt2);
        String type ;
        String money ;
        for(BizWallet wallet : walletList){
            BigDecimal totalType = BigDecimal.ZERO;
            BizMerchant bizMerchant = bizMerchantServiceImpl.getMerchantByUserId(wallet.getUserId());
            if(bizMerchant == null){
                continue;
            }
            BizDayStatistics bds = new BizDayStatistics();
            bds.setUserId(wallet.getUserId());
            bds.setUserName(bizMerchant.getMerchName());
            bds.setProfitAmount(new BigDecimal(DESHelperUtil.decrypt(wallet.getSecretKey(),wallet.getProfitAmount())));
            bds.setRewardAmount(new BigDecimal(DESHelperUtil.decrypt(wallet.getSecretKey(),wallet.getRewardAmount())));
            bds.setTotalWalletAmount(new BigDecimal(DESHelperUtil.decrypt(wallet.getSecretKey(),wallet.getWalletAmount())));
            type = "1";
            money = bizMerchantInfoMapper.getYesterDayStatistics(bizMerchant.getMerchId(),yesterday,type);
            bds.setType1(new BigDecimal(money));
            totalType = totalType.add(new BigDecimal(money));
            type = "2";
            money = bizMerchantInfoMapper.getYesterDayStatistics(bizMerchant.getMerchId(),yesterday,type);
            bds.setType2(new BigDecimal(money));
            totalType = totalType.add(new BigDecimal(money));
            type = "3";
            money = bizMerchantInfoMapper.getYesterDayStatistics(bizMerchant.getMerchId(),yesterday,type);
            bds.setType3(new BigDecimal(money));
            totalType = totalType.add(new BigDecimal(money));
            type = "4";
            money = bizMerchantInfoMapper.getYesterDayStatistics(bizMerchant.getMerchId(),yesterday,type);
            bds.setType4(new BigDecimal(money));
            totalType = totalType.add(new BigDecimal(money));
            type = "5";
            money = bizMerchantInfoMapper.getYesterDayStatistics(bizMerchant.getMerchId(),yesterday,type);
            bds.setType5(new BigDecimal(money));
            totalType = totalType.add(new BigDecimal(money));
            type = "6";
            money = bizMerchantInfoMapper.getYesterDayStatistics(bizMerchant.getMerchId(),yesterday,type);
            bds.setType6(new BigDecimal(money));
            totalType = totalType.add(new BigDecimal(money));
            type = "7";
            money = bizMerchantInfoMapper.getYesterDayStatistics(bizMerchant.getMerchId(),yesterday,type);
            bds.setType7(new BigDecimal(money));
            totalType = totalType.add(new BigDecimal(money));
            type = "8";
            money = bizMerchantInfoMapper.getYesterDayStatistics(bizMerchant.getMerchId(),yesterday,type);
            bds.setType8(new BigDecimal(money));
            totalType = totalType.add(new BigDecimal(money));
            bds.setTotalType(totalType);
            bds.setWalletTime(today);
            bds.setTypeTime(yesterday);
            String dftype =  bizMicroInfoService.getDfTypeByMerchId(bizMerchant.getUserId().toString());
            bds.setVar2(dftype);// A 银付通  B 秒付通/快乐付
            iBizDayStatisticsService.save(bds);
        }
    }


    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //获取昨天的日期
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(new Date());
        rightNow.add(Calendar.DAY_OF_MONTH, -1);
        Date dt1 = rightNow.getTime();
        String format = sdf.format(dt1);
        System.out.println(format);
    }
}
